/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.req.UpdateExaminationPaperRuleRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.resp.GetExaminationPaperRuleResponse;
import com.xy.biz.ts.domain.req.GetExaminationPaperRuleRequest;
import com.xy.biz.ts.domain.req.ListExaminationPaperRuleRequest;
import com.xy.biz.ts.domain.req.SaveExaminationPaperRuleRequest;
import com.xy.biz.ts.domain.query.ExaminationPaperRuleQuery;
import com.xy.biz.ts.domain.converter.ExaminationPaperRuleConverter;
import com.xy.biz.ts.client.ExaminationPaperRuleClient;
import com.xy.biz.ts.service.ExaminationPaperRuleService;

/**
 *
 *
 * @author wwj
 * @date 2021-3-25
 */
@Api(tags = "培训流程服务-试卷规则")
@Slf4j
@RequestMapping("/api/ts/examination_paper_rule")
@RestController
public class ExaminationPaperRuleController implements ExaminationPaperRuleClient {

	@Autowired
	private ExaminationPaperRuleService examinationPaperRuleService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	@ApiOperation("生成规则")
	public Result<?> saveExaminationPaperRule(@RequestBody SaveExaminationPaperRuleRequest request) {
		return examinationPaperRuleService.saveExaminationPaperRule(ExaminationPaperRuleConverter.toBO(request));
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllExaminationPaperRule(@RequestBody Collection<SaveExaminationPaperRuleRequest> collection) {
        return examinationPaperRuleService.saveAllExaminationPaperRule(ExaminationPaperRuleConverter.toBO(collection));
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateExaminationPaperRule(@RequestBody UpdateExaminationPaperRuleRequest request) {
		examinationPaperRuleService.updateExaminationPaperRule(ExaminationPaperRuleConverter.toBO(request));
		return Result.success();
	}

	@Validatable
	@ApiOperation("删除规则")
	@PostMapping(value = "/delete")
	public Result<?> deleteExaminationPaperRule(@RequestBody GetExaminationPaperRuleRequest request) {
        return examinationPaperRuleService.deleteExaminationPaperRule(request.getExaminationPaperRuleId());
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetExaminationPaperRuleResponse> getExaminationPaperRule(@RequestBody GetExaminationPaperRuleRequest request) {
		return Result.success(ExaminationPaperRuleConverter.toResponse(examinationPaperRuleService.getExaminationPaperRule(request.getExaminationPaperRuleId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetExaminationPaperRuleResponse>> listExaminationPaperRuleById(@RequestBody Collection<Long> collection) {
		return Result.success(ExaminationPaperRuleConverter.toResponse(examinationPaperRuleService.listExaminationPaperRuleById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetExaminationPaperRuleResponse>> listExaminationPaperRule(@RequestBody ListExaminationPaperRuleRequest request) {
		return Result.success(ExaminationPaperRuleConverter.toResponse(examinationPaperRuleService.listExaminationPaperRule(ExaminationPaperRuleConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countExaminationPaperRule(@RequestBody ListExaminationPaperRuleRequest request) {
		return Result.success(examinationPaperRuleService.countExaminationPaperRule(ExaminationPaperRuleConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetExaminationPaperRuleResponse>> pageExaminationPaperRule(@RequestBody ListExaminationPaperRuleRequest request) {
		ExaminationPaperRuleQuery query = ExaminationPaperRuleConverter.toQuery(request);
		List<GetExaminationPaperRuleResponse> list = ExaminationPaperRuleConverter.toResponse(examinationPaperRuleService.listExaminationPaperRule(query));
		int count = examinationPaperRuleService.countExaminationPaperRule(query);
		return Result.success(new Page<>(list, count));
	}

}
